<?php

$workers = [];
$workerNum = 2;

for ($i = 0; $i < $workerNum; $i++) {
    $process = new swoole_process("doProcess", false, false);
    $process->useQueue();
    $pid = $process->start();
    $workers[$pid] = $process;
}

function doProcess(swoole_process $process)
{
    $recv = $process->pop();
    echo "从主进程获取到数据: $recv\n";
    sleep(5);
    $process->exit(0);
}

foreach ($workers as $pid => $process) {
    $process->push("hello $pid\n");
}

for ($i = 0; $i < $workerNum; $i++) {
    $ret = swoole_process::wait();
    $pid = $ret['pid'];
    unset($workers[$pid]);
    echo "子进程退出 $pid\n";
}